<?php
declare (strict_types = 1);

namespace app\cli\controller;

use app\backend\model\Article;
use app\BaseController;
use tools\es\MyElasticsearch;

class Es extends BaseController
{
    // 添加文章文档
    public function createArticleDoc()
    {
        try {
            // 实例化ES工具类
            $es = new MyElasticsearch();

            // 取出所有文章
            $i = 0;
            $article = Article::with(['articleContent', 'articleCategory'])
                ->where('status', 3)
                ->limit($i, 10)
                ->select()->toArray();

            foreach ($article as $v) {
                $articleItem = [
                    'id' => $v['id'],
                    'title' => $v['title'],
                    'category' => empty($v['category']) ? '顶级分类' : $v['category'],
                    'content' => strip_tags(htmlspecialchars_decode($v['articleContent']['content'])),
                    'create_time' => $v['create_time']
                ];

                $es->addDoc($articleItem['id'], $articleItem, 'article', '_doc');
            }
        } catch (\Exception $e) {
            $msg = $e->getMessage();
            die($msg);
        }
    }

    // 搜索建议
    public function suggest()
    {
        return view();
    }

    // 获取搜索建议数据接口
    public function suggestData()
    {
        $prefix = trim(input('get.prefix'));

        // 从elasticsearch中搜索
        try {
            // 实例化ES工具类
            $es = new MyElasticsearch();

            // 组装搜索参数体
            $body = [
                '_source' => false,
                'suggest' => [
                    'title_suggest' => [
                        'prefix' => $prefix,
                        'completion' => [
                            'field' => 'title',
                            'skip_duplicates' => true,
                            'size' => 10
                        ]
                    ]
                ]
            ];

            // 进行搜索
            $res = $es->searchDoc('movies', '_doc', $body);
        } catch (\Exception $e) {
            return '服务器异常';
        }

        $resData = $res['suggest']['title_suggest'][0]['options'];
        $resData = array_column($resData, 'text');
        return json($resData);
    }

    public function autoComplete()
    {
        $prefix = trim(input('get.prefix'));

        // 从elasticsearch中搜索
        try {
            // 实例化ES工具类
            $es = new MyElasticsearch();

            // 组装搜索参数体
            $body = [
                '_source' => false,
                'suggest' => [
                    'star_name_suggest' => [
                        'prefix' => $prefix,
                        'completion' => [
                            'field' => 'name',
                            'skip_duplicates' => true,
                            'size' => 10
                        ]
                    ]
                ]
            ];

            // 进行搜索
            $res = $es->searchDoc('stars', '_doc', $body);
        } catch (\Exception $e) {
            return '服务器异常';
        }

        $resData = $res['suggest']['star_name_suggest'][0]['options'];
        $resData = array_column($resData, 'text');
        return json($resData);
    }
}
